<?php

namespace app\admin\model;
use think\Model;
use fast\Tree;
use app\admin\model\ClassSequence;

class Classes extends Model
{
    // 表名
    public $name = 'classes';
    public $prefix_new = 'eb_';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    public $data;
    
    // 定义事件
    protected static function init()
    {
        // 班级创建后自动分配序号
        self::afterInsert(function ($class) {
            // 根据班级类型分配序号
            $classType = $class->classes_type;
            $schoolId = $class->school_id;
            $communityId = $class->community_id;
            
            \app\admin\model\ClassSequence::assignSequence($class->id, $classType, $schoolId, $communityId);
        });
        
        // 班级更新前检查类型是否变化
        self::beforeUpdate(function ($class) {
            // 获取原始数据
            $oldData = self::get($class->id);
            if (!$oldData) {
                return;
            }
            
            // 如果班级类型发生变化，重新分配序号
            if ($oldData['classes_type'] != $class->classes_type) {
                \app\admin\model\ClassSequence::reassignSequence(
                    $class->id, 
                    $class->classes_type, 
                    $class->school_id, 
                    $class->community_id
                );
            }
        });
    }
    
    /**
     * 关联班级序号
     */
    public function sequence()
    {
        return $this->hasOne('ClassSequence', 'class_id', 'id');
    }
    
    /**
     * 获取班级序号
     */
    public function getSequenceNumberAttr($value, $data)
    {
        return \app\admin\model\ClassSequence::getSequenceByClassId($data['id']);
    }
    
    /**
     * 获取格式化的班级序号（带前缀）
     */
    public function getFormattedSequenceAttr($value, $data)
    {
        $sequence = \app\admin\model\ClassSequence::where('class_id', $data['id'])->where('status', 1)->find();
        if (!$sequence) {
            return null;
        }
        
        return \app\admin\model\ClassSequence::getFormattedSequence($sequence['class_type'], $sequence['sequence_number']);
    }
    
    /**
     * 更新班级类型时重新分配序号
     */
    public static function onBeforeUpdate($class)
    {
        // 获取原始数据
        $oldData = self::get($class->id);
        if (!$oldData) {
            return;
        }
        
        // 如果班级类型发生变化，重新分配序号
        if ($oldData['rel_type'] != $class->rel_type) {
            \app\admin\model\ClassSequence::reassignSequence(
                $class->id, 
                $class->rel_type, 
                $class->school_id, 
                $class->community_id
            );
        }
    }


    public function getCity_idTextAttr($value, $data)
    {
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        $s=array();
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            $r = Model("city")->get(array("id" => $nn[$i]));
            if($r) $s[]= $r['name'];
        }
        return join(",",$s);
    }

    public function getClasses_idTextAttr($value, $data=[])
    {
       if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        $s=array();
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            $r = Model("Classes")->get(array("id" => $nn[$i]));
            if($r) $s[]= $r['name']."(classid:{$nn[$i]})";
        }
        return join(",",$s);
    }

    protected function setCity_idAttr($value)
    {
        return $value&&is_array($value)?join(",",$value):$value;
    }

    public function getArea_idTextAttr($value, $data)
    {
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        $s=array();
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            $r = Model("area")->get(array("id" => $nn[$i]));
            if($r) $s[]= $r['name'];
        }
        return join(",",$s);
    }

    protected function setArea_idAttr($value)
    {
        return $value&&is_array($value)?join(",",$value):$value;
    }

    public function getSchool_idTextAttr($value, $data)
    {
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        $s=array();
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            $r = Model("school")->get(array("id" => $nn[$i]));
            if($r) $s[]= $r['name'];
        }
        return join(",",$s);
    }

    protected function setSchool_idAttr($value)
    {
        return $value&&is_array($value)?join(",",$value):$value;
    }

    public function getTeaching_ocation_idTextAttr($value, $data=array())
    {
        $s=array();
        $content=[];
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            if(isset($content[$nn[$i]])) $s[]= $content[$nn[$i]];
        }
        return join(",",$s);
    }

    public function getShow_in_teamTextAttr($value, $data=array())
    {
        $s=array();
        $content=array(
            "1"=>"是",
            "2"=>"否",
            );
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            if(isset($content[$nn[$i]])) $s[]= $content[$nn[$i]];
        }
        return join(",",$s);
    }

    public function getIs_checkTextAttr($value, $data=array())
    {
        $s=array();
        $content=array(
            "1"=>"是",
            "2"=>"否",
            );
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            if(isset($content[$nn[$i]])) $s[]= $content[$nn[$i]];
        }
        return join(",",$s);
    }

    public function getAllow_seeTextAttr($value, $data=array())
    {
        $s=array();
        $content=array(
            "1"=>"允许",
            "2"=>"不允许",
            );
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            if(isset($content[$nn[$i]])) $s[]= $content[$nn[$i]];
        }
        return join(",",$s);
    }

    public function getClass_typeTextAttr($value, $data=array())
    {
        $s=array();
        $content=array(
            "1"=>"学校班级",
            "2"=>"社区班级",
            "3"=>"校外班级",
            "4"=>"组织机构班级",
            "5"=>"自建班级(个人创建的班级)",
            );
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            if(isset($content[$nn[$i]])) $s[]= $content[$nn[$i]];
        }
        return join(",",$s);
    }

    public function getTypeTextAttr($value, $data=array())
    {
        $s=array();
        $content=array(
            "1"=>"学员",
            "2"=>"学校",
            );
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            if(isset($content[$nn[$i]])) $s[]= $content[$nn[$i]];
        }
        return join(",",$s);
    }

    public function getIs_apply_to_joinTextAttr($value, $data=array())
    {
        $s=array();
        $content=array(
            "0"=>"否",
            "1"=>"是",
            );
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            if(isset($content[$nn[$i]])) $s[]= $content[$nn[$i]];
        }
        return join(",",$s);
    }

    public function getCreator_typeTextAttr($value, $data=array())
    {
        $s=array();
        $content=array(
            "1"=>"老师或管理员",
            "2"=>"组织账号",
            );
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            if(isset($content[$nn[$i]])) $s[]= $content[$nn[$i]];
        }
        return join(",",$s);
    }

    public function getOcation_idTextAttr($value, $data=array())
    {
        $s=array();
        $content=[];
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            if(isset($content[$nn[$i]])) $s[]= $content[$nn[$i]];
        }
        return join(",",$s);
    }

    public function getProvice_idTextAttr($value, $data=array())
    {
        $s=array();
        $content=[];
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            if(isset($content[$nn[$i]])) $s[]= $content[$nn[$i]];
        }
        return join(",",$s);
    }

    public function getDistrict_idTextAttr($value, $data=array())
    {
        $s=array();
        $content=[];
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            if(isset($content[$nn[$i]])) $s[]= $content[$nn[$i]];
        }
        return join(",",$s);
    }

    public function getProvince_idTextAttr($value, $data=array())
    {
        $s=array();
        $content=[];
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            if(isset($content[$nn[$i]])) $s[]= $content[$nn[$i]];
        }
        return join(",",$s);
    }

    public function getAddresslv_idTextAttr($value, $data=array())
    {
        $s=array();
        $content=[];
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            if(isset($content[$nn[$i]])) $s[]= $content[$nn[$i]];
        }
        return join(",",$s);
    }

    public function getRel_typeTextAttr($value, $data=array())
    {
        $s=array();
        $content=[];
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            if(isset($content[$nn[$i]])) $s[]= $content[$nn[$i]];
        }
        return join(",",$s);
    }

    public function getRel_idTextAttr($value, $data=array())
    {
        $s=array();
        $content=[];
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            if(isset($content[$nn[$i]])) $s[]= $content[$nn[$i]];
        }
        return join(",",$s);
    }

    public function getCommunity_idTextAttr($value, $data=array())
    {
        $s=array();
        $content=[];
                if($value) {
            $nn=preg_split("/,/",$value);
        }else{
            $nn=array();
        }
        for($i=0;$i<count($nn);$i++) {
            if($nn[$i]=="") continue;
            if(isset($content[$nn[$i]])) $s[]= $content[$nn[$i]];
        }
        return join(",",$s);
    }

}